<?php

require_once($INCLUDE_PATH.'app/controller/InitController.php');
require_once ($INCLUDE_PATH . "/inc/checkfun.php");
require_once ($INCLUDE_PATH . "/include/checklogin.php");
require_once ($INCLUDE_PATH . "/include/WebService2Db.php");
function __autoload($class_name) {
    require_once APP_ROOT.'app/model/'.strtolower($class_name) . '.php';
}
class ItemController extends InitController {

    public function __construct() {
        parent::__construct();

    }


    public function actionAdd() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        Y::$tpl->set("degree",$degree);
        if ($_REQUEST['a']) {
            $s = array();
            $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
            if ($degree) {
                $s['user_id'] = $_REQUEST['user_id'];
            }else {
                $s['user_id'] = $userInfo['id'];
            }

            $s['project_code'] = $_REQUEST['project_code'];
            $s['start_time']=$_REQUEST['start_time'];
            $s['end_time']=$_REQUEST['end_time'];
            $s['state']=md5($_REQUEST['state']);
            $s['title']=$_REQUEST['title'];
            $s['introduction']=$_REQUEST['introduction'];
            $s['created_time']=date("Y-m-d H:i:s");

            $s['created_id']=$userInfo['id'];

            if ($_REQUEST["id"]) {

            }else {
                $s['id'] = "null";
                $result = ItemModel::saveItemInfo($s);
                if ($result) {
                    return self::sysMessage("添加新项目成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
                }else {
                    return self::sysMessage("添加新项目失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
                }
            }
        }
        $usercount = UserModel::getListCount("where user_type = 1");
        $userlist = Usermodel::getList(0,0,$usercount,"where user_type = 1");
        Y::$tpl->set("userlist",$userlist);
    //echo PRE.'hhj';
    }
    public function actionList() {
        $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
        if($_REQUEST['id']) {
            if ($_REQUEST['a'] == "del") {

            //print_r($userType);
                if($userInfo["user_type"] == 2) {
                    $del = ItemModel::delItem($_REQUEST['id']);

                }else {
                    die("<script LANGUAGE='javascript'>alert('对不起，您无权删除！');history.go(-1);</script>");
                }
            }
        }
        if($_REQUEST['p']) {
            $page = $_REQUEST['p'];
        }else {
            $page = 1;
        }
        $pageSize = 20;
        if($userInfo["user_type"] == 2) {
            $where ="";
        }else {

            $where = " where user_id =".$userInfo["id"];
        }
        $sum = ItemModel::getListCount($where);

        if (ceil ( $sum / $pageSize ) <= $page && ceil ( $sum / $pageSize ) != 0) {
            $page = ceil ( $sum / $pageSize );
        }
        $liststart=$pageSize * ($page - 1);
        $result = ItemModel::getList($page,$liststart,$pageSize,$where);
        $par = array();

        $showpage = Site_global::listPage($par, $sum, $page, $pageSize);
        Y::$tpl->set("user_type",$_COOKIE['usertype']);
        Y::$tpl->set("showpage",$showpage);
        Y::$tpl->set("list",$result);
    }
    public function actionEdit() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        $degree = CheckLogin::checkUserDegree();

        $id=$_REQUEST['id'];

        if ($_REQUEST['a'] == d) {
            $s = array();


            $s['project_code'] = $_REQUEST['project_code'];
            $s['user_id'] = $_REQUEST['user_id'];

            $s['start_time']=$_REQUEST['start_time'];
            $s['end_time']=$_REQUEST['end_time'];
            $s['state']=$_REQUEST['state'];
            $s['title']=$_REQUEST['title'];
            $s['introduction']=$_REQUEST['introduction'];
            $s['created_time']=date("Y-m-d H:i:s");
            if ($degree) {
                $where ="where id = ".$id;
            }else {
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                $where = " where id = ".$id." and user_id =".$userInfo['id'];
            }
            $result = ItemModel::editItemInfo($s,$where);
            if ($result) {
                return self::sysMessage("修改项目成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else {
                return self::sysMessage("修改项目失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        }
        $usercount = UserModel::getListCount("where user_type = 1");
        $userlist = Usermodel::getList(0,0,$usercount,"where user_type = 1");
        Y::$tpl->set("userlist",$userlist);
        $itemInfo = ItemModel::getOneItem($id,$degree);
        Y::$tpl->set("item",$itemInfo);
    }
    public function actionCount() {
        if ($_REQUEST['a'] == "do") {
            $start_time = $_REQUEST['start_time'];
            $end_time = $_REQUEST['end_time'];

            $res = ItemModel::getCount($start_time,$end_time);
            Y::$tpl->set("c",$res);
        }
    }
    public function actionUrlCount() {
        $url=self::$conf->confGroup(urlcount);
        if ($_REQUEST['a'] == "do") {

            $start_time = $_REQUEST['start_time'];
            $end_time = $_REQUEST['end_time'];

            $res = ItemModel::getUrlCount($start_time,$end_time,$url);
            Y::$tpl->set("c",$res);

        }
    }




}

?>
